//
//  UIButton+Extension.swift
//  SinaWeibo
//
//  Created by mac on 15/11/29.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

extension UIButton {
    
//    重写构造方法
//    分类中不能够添加指定的构造方法
//    在分类中重写构造方法的时候要使用convenience，调用父类的内容的时候要使用self调用
    convenience init(title : String , backName : String? , color : UIColor, fontSize : CGFloat = 15 , isHightLight : Bool = false , imageName : String? = nil){
        self.init()
        
        if backName != nil {
            
            setBackgroundImage(UIImage(named: backName!), forState: .Normal)
            if isHightLight {
                
                setBackgroundImage(UIImage(named: backName! + "_highlighted"), forState: .Highlighted)
            }
            
        }
        
        if imageName != nil {
            
            setImage(UIImage(named: imageName!), forState: .Normal)
        }
        
        setTitle(title, forState: .Normal)

        setTitleColor(color, forState: .Normal)
        
        titleLabel?.font = UIFont.systemFontOfSize(fontSize)
        
        sizeToFit()
    }
    
    convenience init (imageName: String, backImage: String){
        self.init()
        
        setImage(UIImage(named: imageName), forState: .Normal)
        
       setImage(UIImage(named: imageName + "_highlighted"), forState: .Selected)
        
       setBackgroundImage(UIImage(named: backImage), forState: .Normal)
        
        setBackgroundImage(UIImage(named: backImage + "_highlighted"), forState: .Selected)
        
    }
    
}

